#ifndef __KEY_RECOMMANDER_HH__
#define __KEY_RECOMMANDER_HH__

#include "Reactor/TcpConnection.h"
#include "Dictionary.hh"
#include <string>
#include <vector>
#include <set>
#include <queue>

using std::string;
using std::set;
using std::priority_queue;
using std::vector;

struct MyResult
{
    MyResult(const int& dis, const size_t& f, const string& w)
        : distanse(dis), freq(f), word(w){
    }
    int distanse;
    size_t freq;
    string word;
};


class MyCompare
{
public:
    bool operator()(const MyResult& lhs, const MyResult& rhs) const
    {
        if(lhs.distanse < rhs.distanse){return false;}
        else if (lhs.distanse > rhs.distanse){return true;}
        else{
            if(lhs.freq < rhs.freq){return true;}
            else if(lhs.freq > rhs.freq){return false;}
            else{return false;}
        }
    }
    bool operator()(const pair<int, double>& a, const pair<int, double>& b) const
    {
        return a.second > b.second;
    }
};

class KeyRecommander
{
    using TcpConnectionPtr = shared_ptr<TcpConnection>;
public:
    KeyRecommander(const string& query, TcpConnectionPtr conn);
    void execute();
    void response();
    int distanse(const string& rhs);

private:
    string _queryWord;
    TcpConnectionPtr _conn;
    priority_queue<MyResult, vector<MyResult>, MyCompare> _resultQueue;
};


#endif